﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IniHelper;

namespace LocalConfigFactory
{
    public partial class LocalConfigDialog : Form
    {
        private IniContainer infoContainer;

        public LocalConfigDialog(IniContainer container)
        {
            InitializeComponent();

            this.infoContainer = container;
        }

        private void LocalConfigDialog_Load(object sender, EventArgs e)
        {
            List<string> listHandWriting = infoContainer.getValues("LocalFile", "HandWriting");
            List<string> listWebConfig = infoContainer.getValues("WebFile", "WebConfig");

            if ((listHandWriting == null) || (listHandWriting.Count == 0))
            {
                tbHandWriting.Text = "";
            }
            else
            {
                tbHandWriting.Text = listHandWriting.ElementAt(0);
            }

            if ((listWebConfig == null) || (listWebConfig.Count == 0))
            {
                tbWebConfig.Text = "";
            }
            else
            {
                tbWebConfig.Text = listWebConfig.ElementAt(0);
            }
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            save();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            save();
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnHandWritingChoose_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog handwritingFolderDialog = new FolderBrowserDialog();
            handwritingFolderDialog.SelectedPath = tbHandWriting.Text;
            if (handwritingFolderDialog.ShowDialog() == DialogResult.OK)
            {
                tbHandWriting.Text = handwritingFolderDialog.SelectedPath + "\\";

                infoContainer.overwriteIniValue("LocalFile", "HandWriting", handwritingFolderDialog.SelectedPath + "\\");
            }
        }

        private void btnWebConfigChoose_Click(object sender, EventArgs e)
        {
            OpenFileDialog handwritingFileDialog = new OpenFileDialog();
            handwritingFileDialog.InitialDirectory = tbWebConfig.Text;
            handwritingFileDialog.Filter = "Config File (*.ini)|*.ini";
            if (handwritingFileDialog.ShowDialog() == DialogResult.OK)
            {
                tbWebConfig.Text = handwritingFileDialog.FileName;

                infoContainer.overwriteIniValue("WebFile", "WebConfig", handwritingFileDialog.FileName);
            }
        }

        private void save()
        {
            if ((tbHandWriting.Text == "") || (tbWebConfig.Text == ""))
            {
                MessageBox.Show("Lack of Information!");
                return;
            }
            infoContainer.save();
        }
    }
}
